#version 330 core

uniform sampler2D u_DiffuseTexture;
uniform sampler2D u_NormalTexture;
uniform vec3 u_LigthPos;
uniform float u_LigthIntensity;

in vec3 v_Position;
in vec3 v_Normal;
in vec3 v_Tangent;
in vec3 v_Binormal;
in vec2 v_TexCoord;
in vec3 v_LocalPosition;

out vec4 FragColor;








void main(void)
{
	vec3 v_LocalPosition = v_LocalPosition*0.5 + vec3(0.5);
	
	vec3 eye = normalize(vec3(0) - v_Position);
	vec3 light = u_LigthPos - v_Position;
	float dist = length(light);
	light = normalize(light);
	vec3 texNormal = texture2D(u_NormalTexture, vec2(v_TexCoord)).rgb*2.0 - vec3(1.0);
	vec3 normal = normalize(mat3(v_Tangent, v_Binormal, v_Normal)*texNormal);
	float k_atten = u_LigthIntensity/(pow(dist, 2));
	float k_diffuse = max(dot(light, normal)*k_atten, 0);
	float k_specular = max(pow(dot(reflect(-light, normal), eye), 32.0)*k_atten, 0);
	vec3 ambient = 0.1*v_LocalPosition;//texture2D(u_DiffuseTexture, vec2(v_TexCoord)).rgb;
	vec3 diffuse = k_diffuse*v_LocalPosition;//(texture2D(u_DiffuseTexture, vec2(v_TexCoord)).rgb);
	vec3 specular = k_specular*(vec3(1));
	vec3 color = ambient + diffuse + specular;
	FragColor = vec4(color, 1.0);
}